import unverifiedPort from '@constants/unverifiedPort'
import { getNavigate } from '@utils/navigation'

export default {
  get: async (action, data) => {
    return new Promise(async (resolve, reject) => {
      const params = {
        action,
        data
      };

      try {
        const res = await window.electronAPI.get(params);
        const navigate = getNavigate()

        if (res.code === 401 && !unverifiedPort.includes(action)) {
          localStorage.removeItem('zg-jwt')
          navigate('/login/signIn')
        }

        resolve(res)
      } catch (error) {
        reject(error)
      }
    })
  },
  post: async (action, data) => {
    return new Promise(async (resolve, reject) => {
      const params = {
        action,
        data
      };

      try {
        const res = await window.electronAPI.post(params);
        const navigate = getNavigate()

        if (res.code === 401 && !unverifiedPort.includes(action)) {
          localStorage.removeItem('zg-jwt')
          navigate('/login/signIn')
        }

        resolve(res)
      } catch (error) {
        reject(error)
      }
    })
  }
}